<template>
  <div class="app-container">
      <el-tabs v-model="type" @tab-click="handleClick">
            <el-tab-pane name="1">
              <span slot="label"><i class="el-icon-star-off"></i> 关注时回复</span>
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['mp:reply:add']"
                  >新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
              </el-row>
              <el-table v-loading="loading" border :data="tableData">
                <el-table-column label="序号"  type="index" width="50" align="center" />
                <el-table-column label="回复消息类型" align="center" prop="repType" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.wx_rep_type" :value="scope.row.repType"/>
                  </template>
                </el-table-column>
                <el-table-column label="是否启用" align="center" width="150" prop="isEnable" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
                  </template>
                </el-table-column>
                <el-table-column label="操作"  fixed="right" width="180" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleEdit(scope.row)"
                      v-hasPermi="['mp:reply:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDel(scope.row)"
                      v-hasPermi="['mp:reply:del']"
                    >删除</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-check"
                      @click="handleEnableChg(scope.row.id,1)"
                      v-hasPermi="['mp:reply:edit']"
                      v-if="scope.row.isEnable === 0"
                    >启用</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-close"
                      @click="handleEnableChg(scope.row.id,0)"
                      v-hasPermi="['mp:reply:edit']"
                      v-if="scope.row.isEnable === 1"
                    >禁用</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
              />
            </el-tab-pane>
            <el-tab-pane name="2">
              <span slot="label"><i class="el-icon-chat-line-round"></i> 消息回复</span>
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['mp:reply:add']"
                  >新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
              </el-row>
              <el-table v-loading="loading" border :data="tableData">
                <el-table-column label="序号"  type="index" width="50" align="center" />
                <el-table-column label="请求消息类型" align="center" prop="repType" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.wx_rep_type" :value="scope.row.reqType"/>
                  </template>
                </el-table-column>
                <el-table-column label="回复消息类型" align="center" prop="repType" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.wx_rep_type" :value="scope.row.repType"/>
                  </template>
                </el-table-column>
                <el-table-column label="是否启用" align="center" width="150" prop="isEnable" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
                  </template>
                </el-table-column>
                <el-table-column label="操作"  fixed="right" width="180" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleEdit(scope.row)"
                      v-hasPermi="['mp:reply:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDel(scope.row)"
                      v-hasPermi="['mp:reply:del']"
                    >删除</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-check"
                      @click="handleEnableChg(scope.row.id,1)"
                      v-hasPermi="['mp:reply:edit']"
                      v-if="scope.row.isEnable === 0"
                    >启用</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-close"
                      @click="handleEnableChg(scope.row.id,0)"
                      v-hasPermi="['mp:reply:edit']"
                      v-if="scope.row.isEnable === 1"
                    >禁用</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
              />
            </el-tab-pane>
            <el-tab-pane name="3">
              <span slot="label"><i class="el-icon-news"></i> 关键词回复</span>
              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['mp:reply:add']"
                  >新增</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
              </el-row>
              <el-table v-loading="loading" border :data="tableData">
                <el-table-column label="序号"  type="index" width="50" align="center" />
                <el-table-column label="关键词" align="center" prop="reqKey" />
                <el-table-column label="匹配类型" align="center" prop="repMate" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.wx_rep_mate" :value="scope.row.repMate"/>
                  </template>
                </el-table-column>
                <el-table-column label="回复消息类型" align="center" prop="repType" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.wx_rep_type" :value="scope.row.repType"/>
                  </template>
                </el-table-column>
                <el-table-column label="是否启用" align="center" width="150" prop="isEnable" >
                  <template slot-scope="scope">
                    <dict-tag :options="dict.type.sys_is_enable" :value="scope.row.isEnable"/>
                  </template>
                </el-table-column>
                <el-table-column label="操作"  fixed="right" width="180" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleEdit(scope.row)"
                      v-hasPermi="['mp:reply:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDel(scope.row)"
                      v-hasPermi="['mp:reply:del']"
                    >删除</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-check"
                      @click="handleEnableChg(scope.row.id,1)"
                      v-hasPermi="['mp:reply:edit']"
                      v-if="scope.row.isEnable === 0"
                    >启用</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-close"
                      @click="handleEnableChg(scope.row.id,0)"
                      v-hasPermi="['mp:reply:edit']"
                      v-if="scope.row.isEnable === 1"
                    >禁用</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
              />
            </el-tab-pane>
          </el-tabs>
      <el-dialog :title="handleType == 'add' ? '新增回复消息' : '修改回复消息'" :visible.sync="dialog1Visible" width="50%">
        <el-form label-width="100px">
          <el-form-item label="请求消息类型" v-if="type == '2'">
            <el-select v-model="objData.reqType" placeholder="请选择消息类型" clearable size="small">
              <el-option
                v-for="dict in dict.type.wx_rep_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
                :disabled="dict.disabled"
                v-if="dict.value !== 'event'"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="匹配类型" v-if="type == '3'">
            <el-select v-model="objData.repMate" placeholder="请选择" style="width: 100px">
              <el-option
                v-for="dict in dict.type.wx_rep_mate"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="关键词" v-if="type == '3'">
            <el-input placeholder="请输入内容" v-model="objData.reqKey" clearable> </el-input>
          </el-form-item>
          <el-form-item label="回复消息">
            <WxReplySelect :objData="objData" v-if="hackResetWxReplySelect"></WxReplySelect>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog1Visible = false">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
  import { listReply, editEnable, addReply, updateReply, delReply} from '@/api/mp/reply'
  import WxReplySelect from '@/components/WxMp/WxReply.vue'

  export default {
    name: 'wxautoreply',
    components: {
      WxReplySelect
    },
    dicts: ['wx_rep_mate','wx_rep_type','sys_is_enable'],
    data() {
      return {
        // 显示搜索条件
        showSearch: true,
        tableData: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20, // 每页显示多少条
          ascs:[],//升序字段
          descs:[]//降序字段
        },
        paramsSearch:{},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10
        },
        loading: false,
        dialog1Visible:false,
        type:'1',//类型（1、关注时回复；2、消息回复；3、关键词回复）
        objData:{
          repType : 'text'
        },
        handleType: null,
        dictData: new Map(),
        hackResetWxReplySelect: false
      }
    },
    watch: {

    },
    created() {
      this.getList()
    },
    mounted: function() { },
    computed: {
    },
    methods: {
      handleEnableChg(id,val) {
        console.log('val:' + val);
        editEnable(id,val).then(data => {
          this.$message({
            showClose: true,
            message: '操作成功',
            type: 'success'
          })
          this.getList()
        })
      },
      handleAdd(){
        this.hackResetWxReplySelect = false//销毁组件
        this.$nextTick(() => {
          this.hackResetWxReplySelect = true//重建组件
        })
        this.handleType = 'add'
        this.dialog1Visible = true
        this.objData = {
          repType : 'text'
        }
      },
      handleEdit(row){
        this.hackResetWxReplySelect = false//销毁组件
        this.$nextTick(() => {
          this.hackResetWxReplySelect = true//重建组件
        })
        this.handleType = 'edit'
        this.dialog1Visible = true
        this.objData = Object.assign({}, row)
      },
      handleClick(tab, event){
        this.tableData = []
        this.queryParams.pageNum = 1
        this.type = tab.name
        this.getList()
      },
      searchChange(params,done){
        params = this.filterForm(params)
        this.paramsSearch = params
        this.page.currentPage = 1
        this.getPage(this.page,params)
        done()
      },
      sortChange(val){
        let prop = val.prop ? val.prop.replace(/([A-Z])/g,"_$1").toLowerCase() : '';
        if(val.order=='ascending'){
          this.page.descs = []
          this.page.ascs = prop
        }else if(val.order=='descending'){
          this.page.ascs = []
          this.page.descs = prop
        }else{
          this.page.ascs = []
          this.page.descs = []
        }
        this.getPage(this.page)
      },
      getList() {
        this.loading = true
        this.queryParams.type = this.type;
        listReply(this.queryParams).then(response => {
          this.tableData = response.rows
          this.total = response.total
          this.loading = false
          if(this.type == '2'){
            let wxReqType = this.dictData.get('wx_req_type')
            for (let i=0;i<wxReqType.length;i++) {
              wxReqType[i].disabled = false
              for(let j=0;j<this.tableData.length;j++){
                if(wxReqType[i].value == this.tableData[j].reqType){
                  wxReqType[i].disabled = true
                }
              }
            }
          }
        }).catch(() => {
          this.tableLoading = false
        })
      },
      handleDel: function(row, index) {
        var _this = this
        this.$confirm('是否确认删除此数据', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
            return delReply(row.id)
          }).then(data => {
          _this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        }).catch(function(err) { })
      },
      handleSubmit(row){
        if(this.handleType == 'add'){
        this.objData.type = this.type;
          addReply(this.objData).then(data => {
            this.$message({
              showClose: true,
              message: '添加成功',
              type: 'success'
            })
            this.getList()
            this.dialog1Visible = false
          })
        }
        if(this.handleType == 'edit'){
          updateReply(this.objData).then(data => {
            this.$message({
              showClose: true,
              message: '修改成功',
              type: 'success'
            })
            this.getList()
            this.dialog1Visible = false
          })
        }
      },
      /**
       * @title 数据更新
       * @param row 为当前的数据
       * @param index 为当前更新数据的行数
       * @param done 为表单关闭函数
       *
       **/
      handleUpdate: function(row, index, done, loading) {
        putObj(row).then(data => {
          this.$message({
            showClose: true,
            message: '修改成功',
            type: 'success'
          })
          done()
          this.getPage(this.page)
        }).catch(() => {
          loading()
        })
      },
      /**
       * @title 数据添加
       *
       **/
      handleSave: function(row, done, loading) {
        addObj(Object.assign({
          type:this.type
        }, this.objData)).then(data => {
          this.$message({
            showClose: true,
            message: '添加成功',
            type: 'success'
          })
          this.getPage(this.page)
          this.dialog1Visible = false
        }).catch(() => {
          done()
        })
      },
      /**
       * 刷新回调
       */
      refreshChange(page) {
        this.objData={
            repType : 'text'
        }
        this.getPage(page)
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
